#*************************************************************************************************#
#                                                                                                 #
#                                                                                                 #
#                       REPLICATION CODE FOR THE PAPER:                                           #
#                                                                                                 #
# The Clarity of the Majority's Preference Moderates the Influence of Lobbying on Representation  #
#                                                                                                 #
#                                                                                                 #
#*************************************************************************************************#
#
#***************#
# Load Packages #
#***************#

library(foreign)
library(psych)
library(boot)
library(cocron)
library(reshape)
library(stargazer)
library(lmtest)
library(survey)
library(MASS)
library(Hmisc)
library(lattice)
library(xlsx)
library(xtable)
library(nFactors)
require(xlsReadWrite)
library(GPArotation)
library(lme4)
library(ggplot2)
library(multiwayvcov)


#**************************************************************************************#
#***************#
#   Load data   #
#***************#

setwd("C:\\Users\\jbarc\\Box Sync\\4th term\\Accountability\\Reanalysis\\")

options(java.parameters = "-Xmx512m" )
rep_data <- read.dta("ReplicationData.dta")

#Recoding variable "Closeness of the Referendum" to "Margin of Victory" for the sake of clarity
rep_data$marginofvictory <- -rep_data$closevolk

#**************#
#    Table 1   #
#**************#

#    Column 1   #

col1 <- glmer(defect ~ econ_no + ngo_no + mp_number + partei_einig + marginofvictory + closeparl + bet + months2elec + RefOblig  + RefFak + (1 | mp_canton) + (1 | party), family=binomial, data=rep_data, control = glmerControl(optimizer = "bobyqa", optCtrl=list(maxfun=2e5)))

relgrad <- with(model1@optinfo$derivs,solve(Hessian,gradient))
max(abs(relgrad))#small relative gradient

#    Column 2   #

col2 <- glmer(defect ~ econ_no*marginofvictory + ngo_no*marginofvictory + mp_number + partei_einig + closeparl + bet + months2elec + RefOblig  + RefFak + (1 | mp_canton) + (1 | party), family=binomial, data=rep_data, control = glmerControl(optimizer = "bobyqa", optCtrl=list(maxfun=2e5)))

relgrad <- with(model2@optinfo$derivs,solve(Hessian,gradient))
max(abs(relgrad))#small relative gradient

lrtest(col1, col2)

#    Column 3   #

col3 <- glm(defect ~ (econ_no + ngo_no)*marginofvictory + partei_einig + closeparl + bet + months2elec + RefOblig  + RefFak + factor(mp_canton) + factor(party), family=binomial, data=rep_data)

#    Column 4   #

col4<- glm(defect ~ (econ_no + ngo_no)*marginofvictory + partei_einig + factor(ref_id) + factor(mp_canton) + factor(party), family=binomial, data=rep_data)

#    Column 5   #

col5<-cluster.vcov(col4, rep_data$mp_id)
coeftest(col4, col5)

#*************************************************************************************************#                   
#                                                                                                 #
#            Contact the author at joanbarcelosoler@wustl.edu for either questions or             #
#                          the replication code of the online Appendix                            #
#                                                                                                 #
#*************************************************************************************************#